/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import dao.MenuDao;
import daoimp.MenuDaoImp;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import modelo.dominio.Menu;

/**
 *
 * @author Gera
 */
public class ABMLMenu extends javax.swing.JDialog {

    ArrayList<Menu> listaMenu = new ArrayList<Menu>();
    MenuDao md = new MenuDaoImp();
    Menu m = new Menu();
    /**
     * Creates new form ABMLMenu
     */
    public ABMLMenu(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        listaMenu = md.getAll();
        this.cargarTabla();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblMenu = new javax.swing.JTable();
        btnNuevoMenu = new javax.swing.JButton();
        btnModificarMenu = new javax.swing.JButton();
        btnEliminarMenu = new javax.swing.JButton();
        btnSalirMenu = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(515, 300));

        tblMenu.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nombre", "Precio", "Stock"
            }
        ));
        jScrollPane1.setViewportView(tblMenu);

        btnNuevoMenu.setText("Nuevo");
        btnNuevoMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoMenuActionPerformed(evt);
            }
        });

        btnModificarMenu.setText("Modificar");
        btnModificarMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarMenuActionPerformed(evt);
            }
        });

        btnEliminarMenu.setText("Eliminar");
        btnEliminarMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarMenuActionPerformed(evt);
            }
        });

        btnSalirMenu.setText("Salir");
        btnSalirMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirMenuActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 496, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(btnNuevoMenu)
                        .addGap(44, 44, 44)
                        .addComponent(btnModificarMenu)
                        .addGap(54, 54, 54)
                        .addComponent(btnEliminarMenu)
                        .addGap(42, 42, 42)
                        .addComponent(btnSalirMenu)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNuevoMenu)
                    .addComponent(btnModificarMenu)
                    .addComponent(btnEliminarMenu)
                    .addComponent(btnSalirMenu))
                .addGap(34, 34, 34))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirMenuActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirMenuActionPerformed

    private void btnNuevoMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoMenuActionPerformed
        AltaMenu altaM = new AltaMenu(null, true);
        altaM.setVisible(true);
        altaM.setLocationRelativeTo(this);
        this.limpiarTabla();
        listaMenu = md.getAll();
        this.cargarTabla();
    }//GEN-LAST:event_btnNuevoMenuActionPerformed

    private void btnModificarMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarMenuActionPerformed
        String id = (String) tblMenu.getValueAt(tblMenu.getSelectedRow(), 0);
        m=md.getMenu(Integer.parseInt(id));
        AltaMenu am = new AltaMenu(null, true, m);
        am.setVisible(true);
        am.setLocationRelativeTo(this);
        this.limpiarTabla();
        listaMenu=md.getAll();
        this.cargarTabla();
    }//GEN-LAST:event_btnModificarMenuActionPerformed

    private void btnEliminarMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarMenuActionPerformed
        // TODO add your handling code here:
        try{
           
                String id = (String) tblMenu.getValueAt(tblMenu.getSelectedRow(), 0);
                m=md.getMenu(Integer.parseInt(id));
                int i = JOptionPane.showConfirmDialog(this,"¿Desea Eliminar?","Eliminar",JOptionPane.YES_NO_OPTION);
                if(i == 0){
                    md.delete(m);
                this.limpiarTabla();
                listaMenu=md.getAll();
                this.cargarTabla();
            }
        }catch(ArrayIndexOutOfBoundsException e ){
             JOptionPane.showMessageDialog(this, "Seleccione un elemento de la lista");   
        }
    }//GEN-LAST:event_btnEliminarMenuActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ABMLMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ABMLMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ABMLMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ABMLMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ABMLMenu dialog = new ABMLMenu(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminarMenu;
    private javax.swing.JButton btnModificarMenu;
    private javax.swing.JButton btnNuevoMenu;
    private javax.swing.JButton btnSalirMenu;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblMenu;
    // End of variables declaration//GEN-END:variables
    private void cargarTabla() {
        DefaultTableModel modelo = (DefaultTableModel) tblMenu.getModel();
            String datos[] = new String[4];
            for(Menu m : listaMenu){
                datos[0]= String.valueOf(m.getId());
                datos[1]= m.getNombre();
                datos[2]= String.valueOf(m.getPrecio());
                datos[3]= String.valueOf(m.getStock());
                modelo.addRow(datos);
            }
    }
      private void limpiarTabla() {
        DefaultTableModel modelo = (DefaultTableModel) tblMenu.getModel();
                      while(this.tblMenu.getRowCount() !=0)
                             modelo.removeRow(0);
        
    }
}
